<template>
  <view class="uni-form-item uni-column flex asset-showbox">
    <!-- 资产名称 -->
    <text
      class="flex-1 asset-name"
      :class="{ 'asset-place': !info.showname }"
      >{{ info.showname ? info.showname : '请选择资产' }}</text
    >
    <!-- 扫码 -->
    <AssetScan class="asset-scan" @scansuccess="scansuccess" />
    <!-- 选择资产 -->
    <uni-icons
      class="asset-search"
      type="search"
      size="40"
      @click="selectRepair"
    ></uni-icons>
  </view>
</template>

<script setup>
const props = defineProps({
  info: {
    type: Object,
    default: () => {
      return {
        showname: '',
        name: '',
        groups: '',
        value: '',
      };
    },
  },
});

const emit = defineEmits(['confirm']);

const info = reactive({
  showname: '',
  name: '',
  groups: '',
  value: '',
});

// 扫码成功后的回调
const scansuccess = (data) => {
  console.log('扫码回调 data', data);
  info.value = data[0].seq;
  // info.showname = data[0].asset_code + '-' + data[0].asset_name;
  info.showname = data[0].asset_name;
  emit('confirm', {
    type: 'scan',
    data: info,
  });
};

/* 资产扫码后的返回值
[
  {
    "service_start_date": "",
    "date_of_quality": "",
    "is_competent": "y",
    "production_date": "",
    "asset_parent_seq": "",
    "type": "1434675295501",
    "asset_nature": "1434675295518",
    "asset_name": "奥的斯电梯GEN2-CN-MRL",
    "model_name": "",
    "safety": "org.hibernate.lob.SerializableClob@1706a418",
    "forbid_action": "org.hibernate.lob.SerializableClob@42c210e",
    "install_date": "",
    "location_seq": "1720770043622",
    "seq": "1721116569110",
    "pivotal_key": "1434675295505",
    "dutyuser": "",
    "cost_center_seq": "",
    "createtime": "2024-07-26 16:56:47",
    "snno": "",
    "asset_desc": "org.hibernate.lob.SerializableClob@33332f4f",
    "createuser": "Administrator",
    "desc_png_seq": "1722562267612",
    "unit": "",
    "supplier_seq": "",
    "net_output_value": "",
    "manufacturer_seq": "",
    "asset_code": "SS-DT0-08-004",
    "fixed_asset_code": "",
    "buy_price": "",
    "updatetime": "2024-08-05 15:38:56",
    "updateuser": "Administrator",
    "status": "1434675295508"
  }
]
 */

//
const selectRepair = () => {
  console.log('去选择资产');
  uni.navigateTo({
    url: `/pages/requestRepair/desc/selectAsset?selectSeq=${info.value}`,
    events: {
      selectAssetCallback(data) {
        console.log('资产选择的初步回调data', data);
        info.value = data.value;
        info.groups = data.groups;
        info.showname = data.showname;
        info.name = data.name;

        emit('confirm', {
          type: 'select',
          data: info,
        });

        /*
        {
          "showname": "SG-8010-S-00001-精密机械制造部",
          "name": "精密机械制造部",
          "groups": "位置",
          "value": "1720767331944"
        }
        */
      },
    },
  });
};

watch(
  () => props.info,
  () => {
    info.value = props.info.value;
    info.name = props.info.name;
    info.showname = props.info.showname;
    info.groups = props.info.groups;
  },
  { immediate: true, deep: true }
);
</script>

<style lang="scss" scoped>
.asset-name {
  font-size: 16px;
}
.asset-place {
  color: gray;
}
</style>
